package cn.caixiong.org.controller;


import cn.caixiong.basic.util.AjaxResult;
import cn.caixiong.basic.util.PageList;
import cn.caixiong.org.domain.Department;
import cn.caixiong.org.query.DepartmentQuery;
import cn.caixiong.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.Id;
import javax.validation.Valid;


@RestController//交给Spring管理
@RequestMapping("/dept")
@Api(tags = "部门管理接口", description = "部门管理接口描述")
public class DepartmentController {
    //注入业务层
    @Autowired
    private IDepartmentService departmentService;

    //分页查询
    @PostMapping("/findPage")
    @ApiOperation(value = "分页查询所有部门接口", notes = "分页查询部门列表")
    public PageList<Department> findPage(@RequestBody DepartmentQuery query) {
        //PageList<Department> page = departmentService.findPage(query);
        return departmentService.findPage(query);
    }

    //查询所有
    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有部门接口", notes = "上级部门列表")
    public AjaxResult findAll() {
        return AjaxResult.createSuccess(departmentService.findAll());
    }

    //查询部门树
    @GetMapping("/treeDept")
    @ApiOperation(value = "查询所有部门接口", notes = "上级部门列表")
    public AjaxResult tree() {
        return AjaxResult.createSuccess(departmentService.treeDept());
    }

    //部门查询
    @GetMapping("/departmentMapper")
    @ApiOperation(value = "查询上级部门接口", notes = "部门列表")
    public AjaxResult departmentMapper() {
        return AjaxResult.createSuccess(departmentService.findParentDepts());
    }

    //新增或修改
    @PostMapping
    @ApiOperation(value = "新增修改部门接口", notes = "没有id就是新增")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "department", value = "部门对象", required = true)})
    public AjaxResult saveOrUpdate(@RequestBody @Valid Department department) {
        try {
            departmentService.saveOrUpdate(department);
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return AjaxResult.createSuccess("通过");
    }

    @DeleteMapping("{id}")
    @ApiOperation(value = "删除部门接口", notes = "根据删除id对象")
    public AjaxResult delete(@PathVariable("id") Long Id) {
            departmentService.delete(Id);
            return AjaxResult.createSuccess(null);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除部门接口", notes = "批量删除id对象")
    public AjaxResult batchDelete(@RequestParam("ids") Long[] Ids) {
            departmentService.batchDelete(Ids);
            return AjaxResult.createSuccess(null);
    }


}
